package com.shujia.zookeeper;

import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;

import java.io.IOException;

public class Code01ZookeeperCon {
    public static void main(String[] args) throws IOException, InterruptedException {
        /**
         * connectString – comma separated host:port pairs, each corresponding to a zk server. e.g. "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002" If the optional chroot suffix is used the example would look like: "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002/app/a" where the client would be rooted at "/app/a" and all paths would be relative to this root - ie getting/setting/etc... "/foo/bar" would result in operations being run on "/app/a/foo/bar" (from the server perspective).
         * 超时时间
         * sessionTimeout – session timeout in milliseconds
         * watcher – a watcher object which will be notified of state changes, may also be notified for node events
         *
         */
        int sessionTimeout = 5 * 1000;

        // zookeeper连接信息 只给定其中一个 也可以
        ZooKeeper zkCli = new ZooKeeper("master:2181,node1:2181,node2:2181", sessionTimeout, new Watcher() {
            // 处理监视结果
            @Override
            public void process(WatchedEvent event) {
                System.out.println("监视器被打开... 正在连接...");
            }
        });

        System.out.println("zkCli:"+zkCli);

        zkCli.close();
    }
}
